﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.Popup
{
    //Zeng shunbo
    public partial class PopupARFirePortection : BasePage_SOM
    {
        protected override string MenuID
        {
            get
            {
                return "M140102";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetLanguage();

                this.UCFirePortection1.MenuID = this.MenuID;
                this.UCFirePortection1.CalledBy = 2;
                this.UCFirePortection1.HaveParent = true;

                if (Request.QueryString["ParentStationEventNo"] != null)
                {
                    //this.UCFirePortection1.ParentEventNo = Request.QueryString["ParentStationEventNo"].ToString();
                    this.UCFirePortection1.ParentEventNo = this.Server.UrlDecode(Request.QueryString["ParentStationEventNo"]).ToString();
                }

                if (Request.QueryString["Action"] != null)
                {
                    //this.UCFirePortection1.Action = Request.QueryString["Action"].ToString().ToUpper() == "NEW" ? 1 : 2;
                    this.UCFirePortection1.Action = this.Server.UrlDecode(Request.QueryString["Action"]).ToString().ToUpper() == "NEW" ? 1 : 2;
                }
                else
                {
                    this.UCFirePortection1.Action = 1;
                }

                //if (Request.QueryString["Action"] != null && Request.QueryString["Action"].ToString().ToUpper() == "EDIT")
                if (Request.QueryString["Action"] != null && this.Server.UrlDecode(Request.QueryString["Action"]).ToString().ToUpper() == "EDIT")
                {
                    //this.UCFirePortection1.StationEventID = Convert.ToInt32(Request.QueryString["StationEventID"]);
                    this.UCFirePortection1.StationEventID = Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["StationEventID"]));
                }

                if (Request.QueryString["tempID"] != null)
                {
                    //this.UCFirePortection1.TempID = Convert.ToInt32(Request.QueryString["tempID"]);
                    this.UCFirePortection1.TempID = Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["tempID"]));
                    //this.UCFirePortection1.ParentEventID = Convert.ToInt32(Request.QueryString["ParentStationEventID"]);
                    this.UCFirePortection1.ParentEventID = Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["ParentStationEventID"]));

                    Model.SOM.Event.StationEventInfo parentEventInfo = DAL.SOM.Event.StationEventDa.GetStationEventInfoByID(this.UCFirePortection1.ParentEventID);
                    this.UCFirePortection1.ParentEventNo = parentEventInfo.EventNo;

                }

                //if (Request.QueryString["tempID"] != null && Convert.ToInt32(Request.QueryString["tempID"]) > 0)//if tempID==null set first call =true;
                if (Request.QueryString["tempID"] != null && Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["tempID"])) > 0)//if tempID==null set first call =true;
                {
                    this.UCFirePortection1.FirstCall = false;
                }
                else
                {
                    this.UCFirePortection1.FirstCall = true;
                }

                //Model.SOM.Event.StationEventInfo eventInfo = DAL.SOM.Event.StationEventDa.GetStationEventInfoByID(Convert.ToInt32(Request.QueryString["StationEventID"]));
                Model.SOM.Event.StationEventInfo eventInfo = DAL.SOM.Event.StationEventDa.GetStationEventInfoByID(Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["StationEventID"])));

                if (eventInfo.ParentStationEventID > 0)
                {
                    this.UCFirePortection1.HaveParent = true;
                }

                //if (Request.QueryString["tempID"] != null && Convert.ToInt32(Request.QueryString["tempID"]) > 0)
                if (Request.QueryString["tempID"] != null && Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["tempID"])) > 0)
                {
                    this.UCFirePortection1.HaveParent = true;
                }

                if (eventInfo.FlagObsolete) //if current event has void then set readonly.
                {
                    this.UCFirePortection1.SetReadOnly(true);
                }

                if (eventInfo.EventStatusID > 1) // The event can't edit when current event is completed.
                {
                    this.UCFirePortection1.ReadOnly = true;
                    this.UCFirePortection1.SetReadOnly(true);

                    this.btnSave.Enabled = false;
                    this.btnSaveAndNew.Enabled = false;
                }

                if (eventInfo.ParentStationEventID > 0)//if current event had parent event so can't edit.
                {
                    //this.UCFirePortection1.ReadOnly = true;
                    this.UCFirePortection1.SetReadOnly(true);
                }

                //if (Request.QueryString["ReadOnly"] != null && Convert.ToInt32(Request.QueryString["ReadOnly"]) == 1)//if set read only in AR.
                if (Request.QueryString["ReadOnly"] != null && Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["ReadOnly"])) == 1)//if set read only in AR.
                {
                    this.UCFirePortection1.ReadOnly = true;
                    this.UCFirePortection1.SetReadOnly(true);
                    this.btnSave.Enabled = false;
                    this.btnSaveAndNew.Enabled = false;
                }

                this.UCFirePortection1.SetControlValue();

                //set isolete url.........
                string otherEventUrl = Request.Url.AbsoluteUri;
                //otherEventUrl = otherEventUrl.Substring(0, otherEventUrl.LastIndexOf("/")) + "/FireProtectionEnquiry.aspx?StationEventIDs=";
                //Response.Redirect("../FireProtection/FireProtectionEnquiry.aspx");
                otherEventUrl = APJ.Util.Utility.GetHttpHost(this.Request) + "/SOM/FireProtection/FireProtectionEnquiry.aspx?StationEventIDs=";
                //this.Server.MapPath("../FireProtection/FireProtectionEnquiry.aspx");
                this.UCFirePortection1.OtherEventUrl = otherEventUrl;

                this.UCFirePortection1.SetFromARReadOnly();//set some controls is readonly.

                if (Request.QueryString["hdnInitDateTime"] != null)
                {
                    //U104 Update By Gordon 20150630
                    //this.UCFirePortection1.InitDateTime = this.ConvertToDateTime(Request.QueryString["hdnInitDateTime"]);
                    this.UCFirePortection1.InitDateTime = DateTime.Now;
                }


                //-------------- add at 2008-12-10 (actionlog 0736) ----------------

                //if (Request.QueryString["tempID"] != null && Convert.ToInt32(Request.QueryString["tempID"]) > 0)
                if (Request.QueryString["tempID"] != null && Convert.ToInt32(this.Server.UrlDecode(Request.QueryString["tempID"])) > 0)
                {
                    //----------- modify at 2008-12-31 -----------

                    //delete:
                    //Model.SOM.Event.StationEventInfo tempStationEventInfo = DAL.SOM.Event.StationEventDa.GetStationEventForTempData(Convert.ToInt32(Request.QueryString["tempID"]), this.UCFirePortection1.StationEventID);

                    //if (tempStationEventInfo.EventStatusID > 1)
                    //{
                    //    this.UCFirePortection1.ReadOnly = true;

                    //    this.btnSave.Enabled = false;
                    //}


                    //add:
                    if (eventInfo.EventStatusID > 1)
                    {
                        this.UCFirePortection1.ReadOnly = true;

                        this.btnSave.Enabled = false;
                    }

                    //---------- End modify at 2008-12-31 --------
                }

                if (eventInfo.StationEventID > 0)
                {
                    this.UCFirePortection1.InitDateTime = eventInfo.EventTime;
                }
                //--------------- End add at 2008-12-10 -----------
            }
        }

        private void SetLanguage()
        {
            this.lblLegend.Text = this.GetLabel("SOM_FireProtectionSystemStatus");
            this.btnSave.Text = this.GetLabel("SOM_Confirm");
            this.btnSaveAndNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");

            this.btnSave.Attributes.Add("onclick", "return SaveCheckExistIsolate('" + this.GetLabel("SOM_AutomaticallyAssignTestoreTime") + "');");
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int checkFireZoneGroup = -1;

            //if (this.UCFirePortection1.StationEventID > 0)
            //{
            //    checkFireZoneGroup = 1;
            //}

            if (this.UCFirePortection1.Action == 1)
            {
                checkFireZoneGroup = 1;
            }

            if (this.UCFirePortection1.SaveCheck(checkFireZoneGroup))
            {
                this.UCFirePortection1.btnSave_Click(true);

                string returnVal = "var   arrReturn=new   Array()  ;";
                returnVal = returnVal + " arrReturn[0]=" + this.UCFirePortection1.TempID + ";";
                returnVal = returnVal + " arrReturn[1]=" + this.UCFirePortection1.StationEventID + ";";
                returnVal = returnVal + " window.returnValue=arrReturn;";
                string writeScript = "<script>" + returnVal + "; window.close();</script>";
                Response.Write(writeScript);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int checkFireZoneGroup = -1;

            //if (this.UCFirePortection1.StationEventID > 0)
            //{
            //    checkFireZoneGroup = 1;
            //}

            if (this.UCFirePortection1.Action == 1)
            {
                checkFireZoneGroup = 1;
            }

            if (this.UCFirePortection1.SaveCheck(checkFireZoneGroup))
            {
                this.UCFirePortection1.btnSave_Click(false);

                string returnVal = "var   arrReturn=new   Array()   ;";
                returnVal = returnVal + " arrReturn[0]=" + this.UCFirePortection1.TempID + ";";
                returnVal = returnVal + " arrReturn[1]=" + this.UCFirePortection1.StationEventID + ";";
                returnVal = returnVal + " window.returnValue=arrReturn;";
                string writeScript = "<script>" + returnVal + "; window.close();</script>";
                Response.Write(writeScript);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //-------------huangyao 20090312: those code together with SaveAndAddNew button, this button no use now, so disable those code now
            //string returnVal = "var   arrReturn=new   Array()   ;";
            //returnVal = returnVal + " arrReturn[0]=" + this.UCFirePortection1.TempID + ";";
            //returnVal = returnVal + " arrReturn[1]=" + this.UCFirePortection1.StationEventID + ";";
            //returnVal = returnVal + " window.returnValue=arrReturn;";
            //string writeScript = "<script>" + returnVal + "; window.close();</script>";
            ////-------------huangyao 20090312: end
            //update by gordon 20151028 U225
            string returnVal = "var   arrReturn=new   Array()   ;";
            returnVal = returnVal + " arrReturn[0]=" + this.UCFirePortection1.TempID + ";";
            returnVal = returnVal + " arrReturn[1]=" + this.UCFirePortection1.StationEventID + ";";
            returnVal = returnVal + " window.returnValue=arrReturn;";
            string writeScript = "<script>" + returnVal + "; window.close();</script>";
            //string writeScript = "<script>window.close();</script>";

            Response.Write(writeScript);
        }
    }
}
